package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.SearchColumnPO;
import com.falcon.domain.dos.SearchColumnDO;
import com.falcon.domain.req.SearchColumnQuery;
import com.falcon.domain.req.SearchColumnRequest;

import com.falcon.mapper.ISearchColumnMapper;
import com.falcon.service.ISearchColumnService;


@Slf4j
@Service
public class SearchColumnService implements ISearchColumnService {

    @Autowired
    private ISearchColumnMapper searchColumnMapper;

    @Override
    public Long save(SearchColumnRequest request){
        SearchColumnPO searchColumnPO = BeanUtil.copyProperties(request, SearchColumnPO.class);

        this.searchColumnMapper.insertNotNull(searchColumnPO);

        return searchColumnPO.getId();
    }

    @Override
    public Boolean update(Long id, SearchColumnRequest request){

        SearchColumnPO searchColumnPO = BeanUtil.copyProperties(request, SearchColumnPO.class);
        searchColumnPO.setId(id);

        return this.searchColumnMapper.updateNotNull(searchColumnPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.searchColumnMapper.deleteById(id);
    }

    @Override
    public SearchColumnDO queryById(Long id){

        SearchColumnPO searchColumnPO = this.searchColumnMapper.selectById(id);

        SearchColumnDO searchColumnDO = BeanUtil.copyProperties(searchColumnPO, SearchColumnDO.class);

        return searchColumnDO;
    }


    @Override
    public List<SearchColumnDO> queryList(SearchColumnQuery request){

        Condition condition = request.toCondition();

        List<SearchColumnPO> poList = this.searchColumnMapper.selectList(condition);

        List<SearchColumnDO> doList = BeanUtil.copyToList(poList, SearchColumnDO.class);

        return doList;
    }

    @Override
    public BasePager<SearchColumnDO> queryPager(SearchColumnQuery request){


        PageInfo<SearchColumnPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.searchColumnMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, SearchColumnDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.searchColumnMapper.deleteByIds(ids);
    }


}
